<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Storage;
use Illuminate\Support\Facades\File;
date_default_timezone_set("Asia/Shanghai");
class TestController extends Controller
{
    public function uploadImg(Request $request){
       //判断文件在请求中是否存在，并且文件在上传过程中是否出错
       if($request->hasFile('img') && $request->file('img')->isValid()){
           $file=$request->file('img');

               //$originalName = $file->getClientOriginalName(); // 文件原名
               $ext = $file->getClientOriginalExtension(); // 扩展名
               //$realPath = $file->getRealPath(); //临时文件的绝对路径
               //$type = $file->getClientMimeType(); // audio/wave
               // 上传文件
               $filename = date('Ymd_H_i_s',time()) . '_' . uniqid() . '.' . $ext;
               $save_path='uploads';
               //保存文档到本地
               $file_path = $file->storeAs($save_path, $filename, 'public');
               $local_real_path = storage_path('app/public/' . $file_path);
               //网络访问地址
               //$access_url=Storage::disk('public')->url($save_path.'/'. $filename);
               $successMsg=array('url'=>$local_real_path,'msg'=>1);
               $backMsg=json_encode($successMsg);
               return $backMsg;

       }
    }
}
